﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ModifySeries : System.Web.UI.Page
{
    ManageDatabase data;
    Serie series;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            series = new Serie();
            //User.Identity.Name;
            //CustomIdentity ident = (CustomIdentity)User.Identity;
            //ident.Name, ident.UserType;
            //if (HttpContext.Current.User.IsInRole("page_admin"))

            if (data == null)
            {
                data = new ManageDatabase();
            }
            if (!IsPostBack)
            {
                GetGenres();

                if (Request.QueryString["sorozat"] != null)
                {
                    String s = Request.QueryString["sorozat"].ToString();
                    InitializeEdit(Convert.ToInt32(s));
                }
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
                Page.Form.Attributes.Add("enctype", "multipart/form-data"); 
            }
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
    }

    private void GetGenres()
    {
        ListItemCollection allGenres = data.GetGenres();
        AddSeriesGenres.Items.Clear();
        foreach (ListItem item in allGenres)
        {
            AddSeriesGenres.Items.Add(item);
        }
    }

    protected void SeriesModify_Click(object sender, EventArgs e)
    {
        try
        {
            if (Request.QueryString["sorozat"] != null)
            {
                String s = Request.QueryString["sorozat"].ToString();
                series.id = Convert.ToInt32(s);
            }
            else
            {
                return;
            }
            series.titleHun = AddSeriesTitleHun.Text;
            series.titleOrig = AddSeriesTitleOrig.Text;
            if (series.titleHun == "" || series.titleHun == " ")
            {
                Alert.Show("Kérem adja meg a sorozat címét!");
                return;
            }
            series.genres = Functions.GetSelectedListBoxItems(AddSeriesGenres);
            series.directors = AddSeriesDirectorName.Text;
            series.writers = AddSeriesWriterName.Text;
            series.stars = AddSeriesStarName.Text;
            series.releaseDate = new DateTime(1800, 01, 01);
            if (AddSeriesReleaseDate.Text != "")
            {
                try
                {
                    series.releaseDate = DateTime.Parse(AddSeriesReleaseDate.Text);
                }
                catch (Exception)
                {
                    Alert.Show("Érvénytelen a megjelenés dátuma!");
                    return;
                }
            }
            series.videoUrl = AddSeriesEmbedVideo.Text;
            series.description = AddSeriesDescription.Text;

            bool ImgWasChanged = false;
            series.imgUrl = "";
            if (AddSeriesKeepImgCheck.Checked)
            {
                if (AddSeriesImg.HasFile)
                {
                    try
                    {
                        if (AddSeriesImg.PostedFile.ContentType == "image/pjpeg" || AddSeriesImg.PostedFile.ContentType == "image/x-jpeg") //image/gif,image/x-png
                        {
                            if (AddSeriesImg.PostedFile.ContentLength < 512000)
                            {
                                string filename = "series_" + String.Format("{0:yyyyMMddhhmmss}", DateTime.Now) + ".jpeg";
                                AddSeriesImg.SaveAs(Server.MapPath("~/Pic/") + filename);
                                series.imgUrl = filename;
                                ImgWasChanged = true;
                            }
                            else
                            {
                                StatusLabel.Text = "A maximális képméret 500 kb!";
                                return;
                            }
                        }
                        else
                        {
                            StatusLabel.Text = "Az alábbi képformátum nem támogatott: " + AddSeriesImg.PostedFile.ContentType;
                            return;
                        }
                    }
                    catch (Exception)
                    {
                        StatusLabel.Text = "Hiba történt a kép feltöltése során.";
                        return;
                    }
                }
            }
            else
            {
                ImgWasChanged = false;
            }

            Boolean success = data.UpdateSeries(series, ImgWasChanged);

            if (!success)
            {
                Alert.Show("Hiba történt a sorozat létrehozása során!");
            }
            else
            {
                Response.Redirect("Series.aspx");
            }

        }
        catch (Exception)
        {
            Alert.Show("Hiba történt az sorozat létrehozása során!");
        }

    }
    protected void SeriesCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Series.aspx");
    }

    private void InitializeEdit(int id)
    {
        series = data.GetWholeSerie(id, true);

        if (!User.IsInRole("admin") && Functions.GetUserId() != series.userID && series.status != 1)
        {
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            AddSeriesTitleHun.Text = series.titleHun;
            AddSeriesTitleOrig.Text = series.titleOrig;
            AddSeriesDirectorName.Text = series.directors;
            AddSeriesWriterName.Text = series.writers;
            AddSeriesStarName.Text = series.stars;
            AddSeriesDescription.Text = series.description;
            AddSeriesEmbedVideo.Text = series.videoUrl;
            DateTime nullDate = new DateTime(1800, 01, 01);
            if (!series.releaseDate.Equals(nullDate))
            {
                AddSeriesReleaseDate.Text = String.Format("{0:yyyy-MM-dd}", series.releaseDate);
            }
            foreach (ListItem genre in series.genres)
            {
                foreach (ListItem item in AddSeriesGenres.Items)
                {
                    if (genre.Equals(item))
                    {
                        item.Selected = true;
                    }
                }
            }
            if (!String.IsNullOrEmpty(series.imgUrl)) {
                AddSeriesKeepImg.ImageUrl = "~/Pic/" + series.imgUrl;
            }
        }
    }
    protected void AddSeriesKeepImgCheck_CheckedChanged(object sender, EventArgs e)
    {
        if (AddSeriesKeepImgCheck.Checked)
        {
            AddSeriesKeepImgDiv.Visible = false;
            AddSeriesChangeImgDiv.Visible = true;
        }
        else {
            AddSeriesChangeImgDiv.Visible = false;
            AddSeriesKeepImgDiv.Visible = true;
        }
    }
}